package com.bwie.receman.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Receive;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.AdmissionsRequest;
import com.bwie.common.domain.request.DiagnosticManagementRequest;
import com.bwie.common.domain.request.DiagnosticViewingRequest;
import com.bwie.common.domain.request.ReceiveRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.receman.mapper.RecemanMapper;
import com.bwie.receman.service.RecemanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Log4j2
public class RecemanServiceImpl implements RecemanService {
    @Autowired
    private RecemanMapper recemanMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private HttpServletRequest request;


    @Override
    public Result<PageResult<Receive>> listreception(ReceiveRequest receiveRequest) {

        PageHelper.startPage(receiveRequest.getPageNum(),receiveRequest.getPageSize());
        List<Receive>list=recemanMapper.listreception(receiveRequest);
        PageInfo<Receive> receivePageInfo = new PageInfo<>(list);
        return PageResult.toResult(receivePageInfo.getTotal(),list);
    }

    @Override
    public Result<List<Receive>> healthRecords(Integer receiveId) {
        List<Receive>receiveList=recemanMapper.healthRecords(receiveId);
        return Result.success(receiveList);
    }

    @Override
    public Result  closeConsultation(Integer userId) {
        int i=recemanMapper.closeConsultation(userId);
        if(i>0){
            userBasedID(userId);
            Result.success("操作成功");
        }
        return Result.error();
    }
    @Override
    public Result openConsultation(Integer userId) {
        int i=recemanMapper.openConsultation(userId);
        if(i>0){
//            查询根据用户id查询用户信息
            userBasedID(userId);
            return Result.success("操作成功");
        }
        return Result.error();
    }

    @Override
    public Result<List<Receive>> diagnosticManagement(DiagnosticManagementRequest diagnosticManagementRequest) {

        List<Receive>diagnosticManagement=recemanMapper.diagnosticManagement(diagnosticManagementRequest);

        return Result.success(diagnosticManagement);
    }

    @Override
    public Result<List<Receive>> diagnosticViewing(String receiveDate) {
        List<Receive>diagnosticManagement=recemanMapper.DiagnosticViewingRequest(receiveDate);
        return Result.success(diagnosticManagement);
    }

    @Override
    public Result<List<Receive>> diagnosticViewings( DiagnosticViewingRequest diagnosticViewingRequest) {
        List<Receive>DiagnosticManagements=recemanMapper.DiagnosticViewingRequests(diagnosticViewingRequest);
        return Result.success(DiagnosticManagements);
    }

    @Override
    public Result generateDiagnosticReport(AdmissionsRequest admissionsRequest) {
        Integer receiveId = admissionsRequest.getReceiveId(); // 诊断序号
        String receiveInformation = admissionsRequest.getReceiveInformation();
        int i=recemanMapper.generateDiagnosticReport(receiveId,receiveInformation);
        if(i>0){
//            修改状态
            recemanMapper.modifyStatus(receiveId);
            return Result.success();
        }


        return Result.error();
    }


    private void userBasedID(Integer userId) {
        User user=recemanMapper.selectUser(userId);
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        redisTemplate.delete(userKey);
        User user1 = new User();
        user1.setUserName(user.getUserName());
        user1.setUserId(user.getUserId());
        user1.setConsultationType(user.getConsultationType());
        user1.setRoleId(user.getRoleId());
        user1.setUserAge(user.getUserAge());
        user1.setUserIdentity(user.getUserIdentity());
        user1.setUserSex(user.getUserSex());
        user1.setUserPhone(user.getUserPhone());
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey,JSONObject.toJSONString(user1));
    }



}
